package ca.clarity.hms.rest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import ca.clarity.hms.provider.HmsVillage;

/**
 * Village 에 관련된 RESTful API 를 구현하는 클래스 이다.
 * 
 * @author Junghun, Shin
 * @version 1.0
 */
public class HmsVillageRest {

	public static final String TAG = HmsVillageRest.class.getSimpleName();
	
	/**
	 * village 목록을 가져 온다.
	 * 
	 * @param hmsVillage
	 * @return
	 * @throws org.json.JSONException
	 * @throws java.io.IOException
	 */
	public void  getVillageList() throws JSONException, IOException {
		
		Log.i(TAG, "getVillageList");
		
		String url = "http://webservice.helpmesee.claritydemo.net/HmsAndroidService.asmx/GetVillages";
		
		ArrayList<HmsVillage> result = null;
		
		JSONObject villageParams = new JSONObject();
		
		String jsonString = "";
		
		try {
			URL postUrl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("GET");
	        connection.setRequestProperty("Content-Type", "application/json");
	        
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();

            Log.i(TAG, "success.");
            connection.setConnectTimeout(3000);

            if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                String line = br.readLine();

                Log.i(TAG, "> line : " + line);

                JSONArray jsonArray = new JSONArray(line);
            }
        } catch(Exception e) {

            e.printStackTrace();
			
		}
		
		
		
		
//		try {
//			HttpGet getRequest = new HttpGet(url);
//			DefaultHttpClient client = new DefaultHttpClient();
//			
//			getRequest.setHeader("username", "token");
//			HttpResponse response = client.execute(getRequest);
//			
//			int status = response.getStatusLine().getStatusCode();
//			if(status != HttpStatus.SC_OK) {
//				Log.i(TAG, "success.");
//			} else {
//				Log.i(TAG, "fail.");
//			}
//		} catch(Exception e) {
//			
//		}
		
		
		
		
//		return result;
	}
}
